package com.ctgu.tengyun.manager;

import com.ctgu.tengyun.bean.errorcode.SaleOrderErrorCode;
import com.ctgu.tengyun.bean.request.SparePartApplyRequest;
import com.ctgu.tengyun.config.DataSourcEelectricConfig;
import com.ctgu.tengyun.domain.*;
import com.ctgu.tengyun.exception.ErrorCodeException;
import com.ctgu.tengyun.service.customer.CustomerInfoService;
import com.ctgu.tengyun.service.product.ProductInfoService;
import com.ctgu.tengyun.service.saleorder.SaleOrderConfirmService;
import com.ctgu.tengyun.service.saleorder.SaleOrderRecordService;
import com.ctgu.tengyun.service.sparepart.SparePartInfoService;
import com.ctgu.tengyun.util.StringUtils;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

/**
 * Created by liaoweizhi on 2019/8/7.
 */
@Component
public class SaleOrderManager {

    @Resource
    private ProductInfoService productInfoService;
    @Resource
    private CustomerInfoService customerInfoService;
    @Resource
    private SaleOrderConfirmService saleOrderConfirmService;
    @Resource
    private SaleOrderRecordService saleOrderRecordService;
    @Resource
    private SparePartInfoService sparePartInfoService;

    @Transactional(value = DataSourcEelectricConfig.TX_MANAGER)
    public Long apply(SparePartApplyRequest request, Long userId, SparePartInfo sparePartInfo, String partNo) {
        boolean isReduceStockSuccess = sparePartInfoService.reduceStock(sparePartInfo.getId(), request.getSaleNum());
        if (!isReduceStockSuccess) {
            throw new ErrorCodeException(SaleOrderErrorCode.spare_part_stock_not_enough,
                    SaleOrderErrorCode.spare_part_stock_not_enough.getMsg() + sparePartInfo.getStockNum());
        }

        CustomerInfo customerInfo = new CustomerInfo();
        customerInfo.setName(request.getContactName());
        customerInfo.setContactNo(request.getContactNo());
        customerInfo.setAddress(request.getReceiveAddress());
        customerInfo.setClientStatus(1);
        try {
            customerInfoService.insertSelective(customerInfo);
        } catch (DuplicateKeyException e) {
            CustomerInfo customer = customerInfoService.selectByContactNo(request.getContactNo());
            customerInfo.setId(customer.getId());
        }

        ProductInfo productInfo = productInfoService.selectByProductNo(sparePartInfo.getProductNo());
        SaleOrderRecord record = new SaleOrderRecord();
        record.setName(sparePartInfo.getName());
        record.setProductNo(productInfo.getProductNo());
        record.setPartNo(partNo);
        record.setElectricType(1);
        record.setSaleNum(request.getSaleNum());
        record.setCustomerId(customerInfo.getId());
        record.setContactNo(request.getContactNo());
        record.setApplyTime(new Date());
        record.setReceiveAddress(request.getReceiveAddress());
        if (StringUtils.isNotEmpty(request.getRemark())) {
            record.setRemark(request.getRemark());
        }
        record.setOperationUserId(userId);
        saleOrderRecordService.insertSelective(record);

        SaleOrderConfirm confirm = new SaleOrderConfirm();
        confirm.setSaleRecordId(record.getId());
        confirm.setIsSupport(0);
        confirm.setOrderStatus(1);
        confirm.setOperationUserId(userId);
        saleOrderConfirmService.insertSelective(confirm);

        return record.getId();
    }


}
